package cn.jiyun.controller;

import cn.jiyun.common.config.Result;
import cn.jiyun.entity.Message;

import cn.jiyun.model.User;
import cn.jiyun.service.MessageService;
import cn.jiyun.service.User2Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("message")
@CrossOrigin //允许跨域
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private User2Service user2Service;
    @RequestMapping("findPage")
    public Result findPage(Integer pageNum, Integer pageSize, @RequestBody Message message){
        Page page = messageService.findPage(pageNum,pageSize,message);
        return Result.ok(page);
    }

    @RequestMapping("findPage1")
    public Result findPage1(Integer pageNum, Integer pageSize, @RequestBody User user){
        Page page = user2Service.findPage1(pageNum,pageSize,user);
        return Result.ok(page);
    }

    @RequestMapping("statusUpdate")
    public Result statusUpdate(Integer id,Integer status){
        Message byId = messageService.getById(id);
        byId.setStatus(status);
        return Result.ok(messageService.updateById(byId));
    }

    @RequestMapping("statusUpdate2")
    public Result statusUpdate2(Integer id,Integer caId){
        User byId = user2Service.getById(id);
        byId.setCaId(caId);
        user2Service.updateById(byId);
        return Result.ok(byId.getName());
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        return Result.ok(messageService.getById(id));
    }

    @RequestMapping("add")
    public Result add(@RequestBody Message message){
        if (message.getId()==null){
            messageService.save(message);
        }else {
            messageService.updateById(message);
        }
        return Result.ok();
    }
    @RequestMapping("chios")
    public Result chios(Integer id){
        Message byId = messageService.getById(id);
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getState,1);
        List<User> list = user2Service.list(qw);
        for (User user : list) {
            user.setCaId(1);
            user2Service.updateById(user);
        }
        if (list.size()>0){
            for (User user : list) {
                if (byId.getUserId()==user.getId()){
                   user.setCaId(0);
                    user2Service.updateById(user);
                }
            }

            return Result.ok(list);
        }
        return Result.ok();
    }
}
